package 剑指offer;

public class _18删除链表的节点 {
    public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
 }

    public ListNode deleteNode(ListNode head, int val) {
        if(head.val == val) {
            return head.next;
        }

        ListNode prev = new ListNode(0);
        ListNode node = head;
        prev.next = node;

        while(node != null && node.val != val) {
            prev = node;
            node = node.next;

        }
        if(node.val == val) {
            prev.next = node.next;
            node.next = null;
        }
        return head;
    }

}
